import apis from "../server/FlowMonitorApi";

const { processList, processHandle, TaskSkipList, TaskSkip, taskList, getRuleActionList, BatchTurnTasks, runningVariableList, historicVariableList, updateVariable } = apis;

const initData = {
  processData: [],
  totalCount: 0,
  taskData: [],
  taskTotalCount: 0,
  runningVariableList: {datas: [], totalCount: 0},
  historicVariableList: {datas: [], totalCount: 0},
  // abnormalTotal: 0,
  // executingTotal: 0,
  // finishTotal: 0,
  currentActivity: {},
  newActivity: [],
  RuleTotalCount: 0,
  RuleActionData: [], // 规则操作监控列表
};

export default {
  namespace: "FlowMonitor",
  state: {
    ...initData,
  },

  effects: {
    * getProcessList({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas = [], totalCount } = yield call(processList, payload);
      yield put({ type: "get/processData", payload: datas, totalCount });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    * getTaskList({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas = [], totalCount } = yield call(taskList, payload);
      yield put({ type: "set/taskData", payload: datas, taskTotalCount: totalCount });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    * ProcessHandle({ payload }, { call }) {
      const { errCode, errMsg, errortext } = yield call(processHandle, payload);
      return { errCode, errMsg: errMsg || errortext };
    },
    * getTaskSkipList({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas } = yield call(TaskSkipList, payload);
      yield put({ type: "get/TaskSkipList", payload: datas });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    * getTaskSkip({ payload }, { call }) {
      const { errCode, errMsg, errortext, datas } = yield call(TaskSkip, payload);
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    * getRuleActionList({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas = [], totalCount} = yield call(getRuleActionList, payload);
      yield put({ type: "set/RuleActionData", payload: datas,RuleTotalCount: totalCount });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    * BatchTurnTasks({ payload }, { call }) {
      const { errCode, errMsg, errortext, datas, success } = yield call(BatchTurnTasks, payload);
      return { errCode, errMsg: errMsg || errortext,  datas, success};
    },
    * runningVariableList({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas = [], totalCount } = yield call(runningVariableList, payload);
      yield put({ type: "set/runningVariableList", payload: {datas, totalCount} });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    * historicVariableList({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas = [], totalCount } = yield call(historicVariableList, payload);
      yield put({ type: "set/historicVariableList", payload: {datas, totalCount} });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    * updateVariable({payload}, {call}) {
      const { errCode, errMsg, errortext, datas, success } = yield call(updateVariable, payload);
      return { errCode, errMsg: errMsg || errortext,  datas, success};
    },
  },
  reducers: {
    "get/processData": (state, { payload, totalCount }) => ({
      ...state,
      processData: payload,
      // abnormalTotal: payload.abnormalTotal,
      // executingTotal: payload.executingTotal,
      // finishTotal: payload.finishTotal,
      totalCount,
    }),
    "set/taskData": (state, { payload, taskTotalCount }) => ({
      ...state,
      taskData: payload,
      taskTotalCount,
    }),
    "get/TaskSkipList": (state, { payload }) => ({
      ...state,
      currentActivity: payload.currentActivity,
      newActivity: payload.newActivity,
    }),
    "set/RuleActionData": (state, { payload, RuleTotalCount }) => ({
      ...state,
      RuleActionData: payload,
      RuleTotalCount,
    }),
    "set/runningVariableList": (state, { payload }) => ({
      ...state,
      runningVariableList: payload,
    }),
    "set/historicVariableList": (state, { payload }) => ({
      ...state,
      historicVariableList: payload,
    }),
    clearData: () => ({ ...initData }),
  },
};
